<?php
namespace shenka\controllers;
/**
 * Created by PhpStorm.
 * User: zhang
 * Date: 2015/11/26
 * Time: 11:21
 */
class Select extends Edition{
    public $vars='';
    public $data='';
    /*对父类属性的重写*/
    public $tables='';//数据表信息
    public $connection='';//数据库操作对象
    public $object='';//上一个对象
    public $queue=array();//关键信息使用数组储存
    public function __construct($object){
        $this->object=$object;
        $this->connection=$object->connection;
        $this->tables=$object->tables;
    }
    public function Querys($p){
        return $p;
    }
    protected function GoQuerys($p){
        $this->object->Querys($p);
        $this->queue=$this->object->queue;
    }
    protected function ErrorInfo(){
        $this->queue['警告']='参数错误或者数据不存在！';
        $this->vars=null;
    }
    /*protected function GoSql($p,$table,$field){
        $why=empty($this->object->vars)? $p: $this->object->vars[$field];
        $sql="SELECT * FROM {$this->tables[$table]} WHERE  sn='{$why}'";
        $this->data=$this->OneQuery($sql);
    }*/
    protected function OneQuery($sql){
        $data=$this->connection->createCommand($sql)->queryOne();
        return $data;
    }
    /*protected function AllQuery($sql){
        $data=$this->connection->createCommand($sql)->queryAll();
        return $data;
    }*/
}
class Sn extends Select{
    public function __construct($object){
        if($object instanceof Edition){
            parent::__construct($object);
        }else{
            exit('1此对象不是Edition的子类，不够规范！');
        }
    }
    public function Querys($p){
        $this->GoQuerys($p);
        //根据参数判断sql语句的where条件
        if(!empty($this->object->vars)){
            $why='sn='."'".$this->object->vars['sn']."'";
            return ['id'=>'1'];
        }else{
            $pkey=key($p);
            if($pkey=='sn'){
                $why='sn='."'".$p['sn']."'";
            }elseif($pkey=='logistics'){
                $why='logistics='."'".$p['logistics']."'";
            }elseif($pkey=='id'){
                $why='id='.$p['id'];
            }else{
                $this->ErrorInfo();
            }
        }
        $sql="SELECT * FROM {$this->tables['sn']} WHERE $why ORDER BY id DESC";
        $data=$this->OneQuery($sql);
        if($data){
            $this->vars=$data;
            $this->queue['激活码ID']=$data['id'];
            $this->queue['激活码']=$data['sn'];
            if(isset($data['logistics'])){
                $this->queue['物流码']=$data['logistics'];
            }
        }else{
            $this->ErrorInfo();
        }
        return $this->queue;
    }
}
//单独使用Phone类需要传递user_id参数
class Phone extends Select{
    public function __construct($object){
        if($object instanceof Edition){
            parent::__construct($object);
        }else{
            exit('2此对象不是Edition的子类，不够规范！');
        }
    }
    public function Querys($p){
        $this->GoQuerys($p);
        //根据参数判断sql语句的where条件
        if(!empty($this->object->vars)){
            $why='id='."'".$this->object->vars['user_id']."'";
        }else{
            $pkey=key($p);
            if($pkey=='user_id'){
                $why='id='."'".$p['user_id']."'";
            }elseif($pkey=='phone'){
                $why='phone='."'".$p['phone']."'";
            }else{
                $this->ErrorInfo();
                return $this->queue;
            }
        }
        $sql="SELECT * FROM {$this->tables['user']} WHERE  $why";
        $data=$this->OneQuery($sql);
        if($data){
            $this->vars=$data;
            $this->queue['激活电话']=$data['phone'];
            $this->queue['激活时间']=date('Y-m-d',$data['register_time']);
        }else{
            $this->vars=null;
            $this->ErrorInfo();
        }
        return $this->queue;
    }
}
class PhoneCode extends Select{
    //由于通过手机号查询验证码，在逻辑上不牵扯的其他任何的表，所以这里将去掉一个判断和赋值
    public function __construct($object){
        if($object instanceof Edition){
            parent::__construct($object);
        }else{
            exit('3此对象不是Edition的子类，不够规范！');
        }
    }
    public function Querys($p){
        $sql="SELECT * FROM {$this->tables['code']} WHERE  phone={$p['phone']}";
        $data=$this->OneQuery($sql);
        if($data){
            $this->queue['激活电话']=$data['phone'];
            $this->queue['验证码']=$data['code'];
            $this->queue['添加时间']=date('Y-m-d',$data['addtime']);
        }else{
            $this->ErrorInfo();
        }
        return $this->queue;
    }
}
?>
